﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web;
using Microsoft.SharePoint;
using System.Text;
using System.Linq;

namespace SlamCms.SharePoint.Publishing.WebParts
{
    


    public partial class PublishingContentUserControl : UserControl
    {

        public int PageSize{ get; set; }

        public int DaysScope { get; set; }

        public PublishingContentStatus StatustToShow { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterClientScriptInclude("jquery", ResolveUrl("~/_layouts/inc/jquery-1.6.2.min.js"));
            Page.ClientScript.RegisterClientScriptInclude("datatables", ResolveUrl("~/_layouts/inc/jquery.dataTables.js"));
            AddCssInclude("/Style Library/SlamCMS/css/SlamCMS_table.css");
           

            this.ltInitScript.Text = String.Format("<script type=\"text/javascript\">{0}</script>", GetScript());
        }

        private void AddCssInclude(string url)
        {
            string include = String.Format("<link rel=\"stylesheet\" type=\"text/css\" href=\"{0}\" media=\"all\" />", ResolveUrl(url));
            if (!Page.Header.Controls.Cast<Control>().OfType<LiteralControl>().Any(c => c.Text == include))
            {
                Page.Header.Controls.Add(new LiteralControl(include));
            }
        }

        private string GetScript()
        {

            string status="";
             switch (this.StatustToShow)
            { 
                case PublishingContentStatus.Draft:
                    status = "Draft";
                    break;
                case PublishingContentStatus.Published:
                    status= "Published";
                    break;
                default:
                    status = "";
                    break;
            }
             string userName = HttpContext.Current.User.Identity.Name.Contains("|") ? HttpContext.Current.User.Identity.Name.Split('|').Last() : HttpContext.Current.User.Identity.Name;
            return  String.Format("$(document).ready(initializeTable('{0}',{1},'{2}','{3}',{4}));",this.ContentTableContainer.ClientID,this.PageSize,userName.Replace("\\","\\\\"),status,this.DaysScope);

          
        }
    }
}
